/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;

/**
 *
 * @author Jeppe
 */
public class MainFrame extends javax.swing.JFrame {

    private static final char DELIMITER = 172;
    private Timer t;
    private Socket socket;
    private InputStream inputStream;
    private OutputStream outputStream;
    private Scanner scan;
    private PrintWriter pw;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        setSize(818, 500);

        t = new Timer(5000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                titelLabel.setText("Scan dit kort for registrering..");
                navneLabel.setText("");
                t.stop();
            }
        });
        try {
            socket = new Socket("localhost", 8189);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            scan = new Scanner(inputStream);
            pw = new PrintWriter(outputStream);
        } catch (IOException ex) {
            System.out.println("FÅCK!");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        titelLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        navneLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        getContentPane().setLayout(null);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jTextField1.setBorder(null);
        jTextField1.setOpaque(false);
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        titelLabel.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        titelLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titelLabel.setText("Scan venligst dit kort ");

        jLabel2.setText("ID:");

        navneLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        navneLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        navneLabel.setText("Scan venligst dit kort ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(jLabel2)
                        .addGap(28, 28, 28)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(titelLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 758, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(navneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 758, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(titelLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(navneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(232, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 820, 500);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        t.stop();
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            lookUp();
            jTextField1.setText("");
        }
        t.start();
    }//GEN-LAST:event_jTextField1KeyPressed

    private void lookUp() {
        if (false) {
            //<editor-fold defaultstate="collapsed" desc="Hardcoding">
            if (jTextField1.getText().equals("B4C740E7")) {
                titelLabel.setText("Velkommen til EASJ,");
                navneLabel.setText("Malene L. Madsen!");
            } else if (jTextField1.getText().equals("1064E68B")) {
                titelLabel.setText("Velkommen til EASJ,");
                navneLabel.setText("Jeppe A. Jensen!");
            }
//</editor-fold>
        } else {
            if (socket != null) {
                if (socket.isConnected()) {
                    pw.println("ID"+DELIMITER + jTextField1.getText());
                    String response = scan.nextLine();
                    System.out.println(response);
                    String id = response.split(DELIMITER + "")[0];
                    String name = response.split(DELIMITER + "")[1] + response.split(DELIMITER + "")[2];
                    titelLabel.setText("Velkommen til EASJ,");
                    navneLabel.setText(name + "!");
                }
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel navneLabel;
    private javax.swing.JLabel titelLabel;
    // End of variables declaration//GEN-END:variables
}
